﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Configuration;
using System.Data;
using Logging;

namespace DAL
{
    public class MySQLHelper
    {
        public static readonly string adminConnectionString = ConfigurationManager.ConnectionStrings["adminMySqlHelperConnectionString"].ConnectionString;
        public static readonly string publicConnectionString = ConfigurationManager.ConnectionStrings["MySqlHelperConnectionStringPublic"].ConnectionString;
        public static readonly string ShopConnectionString = ConfigurationManager.ConnectionStrings["MySqlHelperConnectionStringPublic"].ConnectionString;
        /// <summary>
        /// 数据超级管理连接
        /// </summary>
        [ThreadStatic]
        public static MySqlConnection conn = null;
        /// <summary>
        /// 公共库数据库连接
        /// </summary>
        [ThreadStatic]
        public static MySqlConnection publicconn = null;
        /// <summary>
        /// 创建超级管理连接
        /// </summary>
        /// <returns></returns>
        protected static MySqlConnection CreateAdminConnection()
        {
            if (conn == null)
            {
                conn = new MySqlConnection(adminConnectionString);
            }
            return conn;
        }
        /// <summary>
        /// 创建公共库连接
        /// </summary>
        /// <returns></returns>
        protected static MySqlConnection CreatePublicConnection()
        {
            if (publicconn == null)
            {
                publicconn = new MySqlConnection(publicConnectionString);
            }
            return publicconn;
        }
        /// <summary>
        /// 创建店铺库连接
        /// </summary>
        /// <returns></returns>
        protected static MySqlConnection CreateShopConnection()
        {
            MySqlConnection shopconn = null;
            if (shopconn == null)
            {
                shopconn = new MySqlConnection(ShopConnectionString);
            }
            return shopconn;
        }
        /// <summary>
        /// 创建店铺库连接
        /// </summary>
        /// <returns></returns>
        protected static MySqlConnection CreateShopConnection(int ShopId)
        {
            MySqlConnection shopconn = null;
            if (shopconn == null)
            {
                shopconn = new MySqlConnection(ShopConnectionString);
            }
            return shopconn;
        }
        #region MySqlHelper方法
        public DataSet ExecuteAdminDataSet(string commandText, params object[] parameters)
        {
            conn = CreateAdminConnection();
            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
            DataSet ds = MySqlHelper.ExecuteDataset(conn, string.Format(commandText, parameters));
            conn.Close();
            return ds;
        }

        public int ExecuteAdminNonQuery(string commandText, params object[] parameters)
        {
            conn = CreateAdminConnection();
            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
            string s = string.Format(commandText, parameters);
            int result = MySqlHelper.ExecuteNonQuery(conn, string.Format(commandText, parameters));
            conn.Close();
            return result;
        }

        public int ExecuteAdminNonQuery(string commandText)
        {
            conn = CreateAdminConnection();
            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
            int result = MySqlHelper.ExecuteNonQuery(conn, commandText);
            conn.Close();
            return result;
        }

        public object ExecuteAdminScalar(string commandText, params object[] parameters)
        {
            conn = CreateAdminConnection();
            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
            string sql = string.Format(commandText, parameters);
            object result = MySqlHelper.ExecuteScalar(conn, string.Format(commandText, parameters));
            conn.Close();
            return result;
        }
        #endregion

        #region MySqlHelper方法
        public DataSet ExecutePublicDataSet(string commandText, params object[] parameters)
        {
            publicconn = CreatePublicConnection();
            if (publicconn.State != ConnectionState.Open)
            {
                publicconn.Open();
            }
            DataSet ds = MySqlHelper.ExecuteDataset(publicconn, string.Format(commandText, parameters));
            publicconn.Close();
            return ds;
        }

        public int ExecutePublicNonQuery(string commandText, params object[] parameters)
        {
            publicconn = CreatePublicConnection();
            if (publicconn.State != ConnectionState.Open)
            {
                publicconn.Open();
            }
            string s = string.Format(commandText, parameters);
            int result = MySqlHelper.ExecuteNonQuery(publicconn, string.Format(commandText, parameters));
            publicconn.Close();
            return result;
        }

        public int ExecutePublicNonQuery(string commandText)
        {
            publicconn = CreatePublicConnection();
            if (publicconn.State != ConnectionState.Open)
            {
                publicconn.Open();
            }
            int result = MySqlHelper.ExecuteNonQuery(publicconn, commandText);
            publicconn.Close();
            return result;
        }

        public object ExecutePublicScalar(string commandText, params object[] parameters)
        {
            publicconn = CreatePublicConnection();
            if (publicconn.State != ConnectionState.Open)
            {
                publicconn.Open();
            }
            string sql = string.Format(commandText, parameters);
            object result = MySqlHelper.ExecuteScalar(publicconn, string.Format(commandText, parameters));
            publicconn.Close();
            return result;
        }
        #endregion

        #region MySqlHelper方法
        /// <summary>
        /// 
        /// </summary>
        /// <param name="commandText"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static DataSet ExecuteShopDataSet(string commandText, params object[] parameters)
        {
            MySqlConnection shopconn = null;
            shopconn = CreateShopConnection();
            if (shopconn.State != ConnectionState.Open)
            {
                shopconn.Open();
            }
            string sql = commandText;
            if (parameters != null)
            {
                sql = string.Format(sql, parameters);
            }

            ILogger log = Logging.LogManager.GetInstance().GetLogger();
            log.Error(sql);

            DataSet ds = MySqlHelper.ExecuteDataset(shopconn, sql);
            shopconn.Close();
            return ds;
        }
        public static MySql.Data.MySqlClient.MySqlDataReader ExecuteShopDataReader(string commandText, params object[] parameters)
        {
            MySqlConnection shopconn = null;
            shopconn = CreateShopConnection();
            if (shopconn.State != ConnectionState.Open)
            {
                shopconn.Open();
            }
            string sql = commandText;
            if (parameters != null)
            {
                sql = string.Format(sql, parameters);
            }
            return MySqlHelper.ExecuteReader(shopconn, sql);
        }

        //【codereview：这里不需要传入ShopID的，你看下CreateShopConnection这个方法中，已经有自己获取ShopID了，看本类文件46行 --- fangm】
        /// <summary>
        /// 执行Sql语句，返回受影响的条数
        /// </summary>
        /// <param name="commandText"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static int ExecuteShopNonQuery(int shopID, string commandText, params object[] parameters)
        {
            MySqlConnection shopconn = null;
            shopconn = CreateShopConnection(shopID);
            if (shopconn.State != ConnectionState.Open)
            {
                shopconn.Open();
            }
            string sql = commandText;
            if (parameters != null)
            {
                sql = string.Format(sql, parameters);
            }
            int result = MySqlHelper.ExecuteNonQuery(shopconn, sql);
            shopconn.Close();
            return result;
        }
        #endregion
    }
}
